﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Taurus.Platform.BLL;
using Taurus.Platform.UI;
using Taurus.Platform.DTO;
using Taurus.Platform.Net;
using Newtonsoft.Json.Linq;
using Taurus.Parking.BLL;
using Taurus.Parking.DTO;
using Taurus.Platform.XML;
using Taurus.Parking.XML;

namespace Taurus.Parking.Setting
{
    public partial class ParkingSetting : UserControl
    {
        private AccountBll _account = new AccountBll();
        private IPersistence<ParkingUser> _per = new ParkingUserPersistence();
        private IPersistence<Config> _pconfig = new ConfigPersistence();

        public ParkingSetting()
        {
            InitializeComponent();
            dgvFriendsGarage.AutoGenerateColumns = false;
        }

        private void ParkingSetting_Load(object sender, EventArgs e)
        {
            cbxAccount.DataSource = _account.GetUserAccount();
            cbxAccount.DisplayMember = "Name";
        }


        private void buttonSpecAny1_Click(object sender, EventArgs e)
        {
            //添加指定好友
            ParkingUser ac = _per.GetObject(((UserAccount)cbxAccount.SelectedItem).UID);
            if (ac == null)
            {
                ac = new ParkingUser((UserAccount)cbxAccount.SelectedItem);
            }
            MyGarage mg = new MyGarage(ac);
            FriendDialog fd = new FriendDialog(ac);
            if (fd.ShowDialog() == DialogResult.OK)
            {
                ac.PriorityUsers.Add(fd.SelectFriend.UID);
                _per.Update(ac);
                //TODO：添加指定好友
                ParkingUser friend = new ParkingUser(fd.SelectFriend);
                mg.InitUserAccountData(friend);
                _per.Update(friend);
                _per.Save();
                BindPriorityUsersDgv();
            }
        }

        private void BindPriorityUsersDgv()
        {
            UserAccount ua = (UserAccount)cbxAccount.SelectedItem;
            ParkingUser pu = _per.GetObject(ua.UID);
            if (pu != null)
            {
                var ds = from account in pu.WrapperPriorityUsers select new { Name = account.Name, Count = 4, MM = account.Garage.MoneyMinute };
                dgvFriendsGarage.DataSource = ds.ToList();
            }
        }

        private void cbxAccount_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindPriorityUsersDgv();
            UserAccount ua = (UserAccount)cbxAccount.SelectedItem;
            Config cf = _pconfig.GetObject(ua.UID);
            if (cf==null)
            {
                cf = new Config(ua.UID);
                _pconfig.Update(cf);
                _pconfig.Save();
            }
            if (!cf.IsAutoParking)
            {
                rbtnCustomPark.Checked = true;
            }
            else
            {
                rbtnAutoPark.Checked = true;
            }
            numberUpDown.Value = cf.AutoUpdateInterval;
           
        }

        private void rbtnAutoPark_CheckedChanged(object sender, EventArgs e)
        {
            SaveConfig();
            pnlUserPark.Enabled = rbtnCustomPark.Checked;
        }

        private void SaveConfig()
        {
            UserAccount ua = (UserAccount)cbxAccount.SelectedItem;
            Config cf = _pconfig.GetObject(ua.UID);
            if (cf == null)
            {
                cf = new Config(ua.UID);
            }
            cf.IsAutoParking = rbtnAutoPark.Checked;
            cf.AutoUpdateInterval = (int)numberUpDown.Value;
            _pconfig.Update(cf);
            _pconfig.Save();
        }

        private void numberUpDown_Leave(object sender, EventArgs e)
        {
            SaveConfig();
        }
    }
}
